package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.app.entity.CouponsConfig;
import org.springblade.modules.app.service.ICouponsConfigService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 优惠券配置 控制器
 *
 * @author BladeX
 * @since 2020-10-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/couponsconfig")
@Api(value = "优惠券配置", tags = "优惠券配置接口")
public class CouponsConfigController extends BladeController {

    private final ICouponsConfigService couponsConfigService;


    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入couponsConfig")
    public R<CouponsConfig> detail(CouponsConfig couponsConfig) {
        CouponsConfig detail = couponsConfigService.getOne(Condition.getQueryWrapper(couponsConfig));
        return R.data(detail);
    }

    /**
     * 分页 优惠券配置
     */
    @GetMapping("/list")
    public R<IPage<CouponsConfig>> list(Page page, CouponsConfig couponsConfig) {
        LambdaQueryWrapper<CouponsConfig> queryWrapper = new LambdaQueryWrapper<>();
        IPage<CouponsConfig> pages = couponsConfigService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 自定义分页 优惠券配置
     */
    @GetMapping("/page")
    public R<IPage<CouponsConfig>> page(Page page, CouponsConfig couponsConfig) {
        LambdaQueryWrapper<CouponsConfig> queryWrapper = new LambdaQueryWrapper<>();
        IPage<CouponsConfig> pages = couponsConfigService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 新增 优惠券配置
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入couponsConfig")
    public R save(@Valid @RequestBody CouponsConfig couponsConfig) {
        return R.status(couponsConfigService.save(couponsConfig));
    }

    /**
     * 修改 优惠券配置
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入couponsConfig")
    public R update(@Valid @RequestBody CouponsConfig couponsConfig) {
        return R.status(couponsConfigService.updateById(couponsConfig));
    }

    /**
     * 新增或修改 优惠券配置
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入couponsConfig")
    public R submit(@Valid @RequestBody CouponsConfig couponsConfig) {
        return R.status(couponsConfigService.saveOrUpdate(couponsConfig));
    }


    /**
     * 删除 优惠券配置
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(couponsConfigService.removeByIds(Func.toLongList(ids)));
    }


}
